package com.gis3c.sys.service.impl;

import com.gis3c.sys.dao.OrgDao;
import com.gis3c.sys.entry.po.Organization;
import com.gis3c.sys.entry.vo.OrganizationModel;
import com.gis3c.sys.service.OrgService;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by hukekuan on 17-12-6.
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class OrgServiceImpl implements OrgService {

    @Autowired private Mapper baseMapper;
    @Resource private OrgDao orgDao;

    @Override
    @Transactional(propagation= Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public void createOrg(OrganizationModel org) {
        Organization newOrg = baseMapper.map(org,Organization.class);
        newOrg.setOrgId(UUID.randomUUID().toString());
        newOrg.setCreatedTime(new Date());
        orgDao.createOrg(newOrg);
    }

    @Override
    public void updateOrg(OrganizationModel org) {

    }

    @Override
    public int deleteOrg(String orgId) {
        return 0;
    }

    @Override
    public int deleteOrgs(String... orgId) {
        return 0;
    }

    @Override
    public OrganizationModel findOrgById(String orgId) {
        return null;
    }

    @Override
    public int orgCount(String orgName) {
        return 0;
    }

    @Override
    public List<Map<String,Object>> orgList() {
        List<Map<String,Object>> result = new ArrayList<>(),secondResult;
        Map<String,Object> treeNode,secondNode;
        List<Organization> queryOrgList = orgDao.orgList();

        List<Organization> firstLevel = queryOrgList.stream().filter(org -> "0".equals(org.getParentId())).collect(Collectors.toList());
        List<Organization> secondLevel;
        if(firstLevel.size() > 0){
            for(Organization org : firstLevel){
                treeNode = new HashMap<>();
                treeNode.put("id",org.getOrgId());
                treeNode.put("name",org.getOrgName());

                secondLevel = queryOrgList.stream()
                        .filter(second -> org.getOrgId().equals(second.getParentId()))
                        .collect(Collectors.toList());
                if(secondLevel.size() > 0){
                    secondResult = new ArrayList<>();
                    for(Organization secondOrg :secondLevel){
                        secondNode = new HashMap<>();
                        secondNode.put("id",secondOrg.getOrgId());
                        secondNode.put("name",secondOrg.getOrgName());

                        secondResult.add(secondNode);
                    }
                    treeNode.put("children",secondResult);
                }

                result.add(treeNode);
            }
        }


        return result;
    }
}
